/**********************************************************
*
*   @file    useri2cReg.c
*   @author  Derrick Wang
*   @time    2018-4-26
*		@brief   i2c协议和外部交互的函数
*
*
***********************************************************/


#include "main.h"
#include "stm32f0xx_hal.h"
#include "userI2c.h"
#include "userI2cReg.h"
#include "tim.h"
#include "usersmg.h"

smgClass smg;

struct userI2cRegStruct userI2cRegList[]=
{
	//通用寄存器
	{REG_CHECK,1,CMD_RT_TRANSMIT},
	{REG_VERSION,1,CMD_RT_TRANSMIT},
	{REG_LED,1,CMD_RT_RECEIVE},
	{REG_RESET,0,CMD_RT_RECEIVE},
	//模块专用寄存器
	{REG_DIS_MODE,1,CMD_RT_RECEIVE},
	{REG_NORMAL_DIS_DATA,1,CMD_RT_RECEIVE},
	{REG_ROLL_DIS_DATA,1,CMD_RT_RECEIVE},
	{REG_LIGHTENESS,1,CMD_RT_RECEIVE},
	{REG_ONEBIT_NUMBER,1,CMD_RT_RECEIVE},
	{REG_ONEBIT_CODE,1,CMD_RT_RECEIVE},
	{REG_ONEBIT_BLINK,1,CMD_RT_RECEIVE},
	{REG_CLEAR_BLINK,1,CMD_RT_RECEIVE},
	{REG_CLEAR_DISPLAY,1,CMD_RT_RECEIVE},
};

//获取要发送的数据，返回-1为错误，其他值为返回的数据长度
//cmd为命令，buf用来存放获取到的数据
uint8_t user_I2cGetTransmitData(uint8_t reg,uint8_t *buf)
{
	switch(reg)
	{
		case REG_CHECK:
			buf[0]=REG_CHECK_REPLY;
			break;
		case REG_VERSION:
			buf[0]=VERSION;
			break;
	}
	return 1;
}
//命令执行函数
void user_I2cExecuteCallBack(uint8_t reg,uint8_t *buf,uint8_t size)
{
	switch(reg)
	{
		case REG_LED:
			if(buf[0]==0x00)
			{
				LED_OFF();
			}
			else
			{
				LED_ON();
			}
			break;
		case REG_RESET:
			NVIC_SystemReset();
			break;
		case REG_DIS_MODE:
			smg.setDisMode(buf[0]);
			break;
		case REG_NORMAL_DIS_DATA:
			smg.setNormalDisNumber(buf+1,buf[0]);
			break;
		case REG_ROLL_DIS_DATA:
			smg.setRollDisNumber(buf+1,buf[0]);
			break;
		case REG_LIGHTENESS:
			smg.setLighteness(buf[0]);
			break;
		case REG_ONEBIT_NUMBER:
			smg.setOneBitNumber(buf[0],buf[1]);
			break;
		case REG_ONEBIT_CODE:
			smg.setOneBitCode(buf[0],buf[1]);
			break;
		case REG_ONEBIT_BLINK:
			smg.setBlinkOneBit(buf[0]);
			break;
		case REG_CLEAR_BLINK:
			smg.clearBlink();
			break;
		case REG_CLEAR_DISPLAY:
			smg.clearDis();
			break;
	}
}


